<template>
	<view class="gradens-xcjh-report">
		<view class="gradens-yhrw-view-card">
			<view class="yhrw-view-list-item">
				<view>{{patrolPlanInfo.planName}}</view>
			</view>
			<view class="" style="border-bottom: 1px solid #c5c5c5;margin-top: 20rpx;"></view>
			<view class="yhrw-view-list-item">
				<view class="yhrw-view-list-name">巡查开始时间:</view>
				<text class="yhrw-view-list-text">{{patrolPlanInfo.patrolStartTime | date("yyyy年mm月dd日")}}</text>
			</view>
			<view class="yhrw-view-list-item">
				<view class="yhrw-view-list-name">巡查结束时间:</view>
				<text class="yhrw-view-list-text">{{patrolPlanInfo.patrolEndTime | date("yyyy年mm月dd日")}}</text>
			</view>
			<view class="yhrw-view-list-item">
				<view class="yhrw-view-list-name" style="width: 125rpx;">巡查类型:</view>
				<text class="yhrw-view-list-text">{{patrolPlanInfo.patrolType}}</text>
				<view class="yhrw-view-list-name" style="width: 125rpx;">次数要求:</view>
				<text class="yhrw-view-list-text">{{patrolPlanInfo.numberRequire}}</text>
			</view>

			<view class="" style="border-bottom: 1px solid #c5c5c5;margin-top: 20rpx;"></view>
			<view class="" style="margin-top: 20rpx;">
				<uni-forms ref="form" :label-width="100" :modelValue="formData" :rules="rules">
					<uni-forms-item label="巡查录入日期:" name="patrolTime">
						<uni-datetime-picker type="date" :clear-icon="false" :start='patrolPlanInfo.patrolStartTime' :end='patrolPlanInfo.patrolEndTime' v-model="formData.patrolTime" />
					</uni-forms-item>
					<uni-forms-item label="是否异常:" name="isAbnormal">
						<uni-data-checkbox style="margin-top: 10rpx;" :multiple="false" v-model="formData.isAbnormal"
							:localdata="range"></uni-data-checkbox>
					</uni-forms-item>
					<uni-forms-item label="巡查说明:" name="patrolRecord">
						<uni-easyinput class="input" v-model="formData.patrolRecord" type="text"
							placeholder="请输入巡查说明"></uni-easyinput>
					</uni-forms-item>
					<uni-forms-item label="巡查距离:" name="patrolRecord">
						<text style="line-height: 70rpx;">暂无数据</text>
					</uni-forms-item>
					<uni-forms-item label="巡查速度:" name="patrolRecord">
						<text style="line-height: 70rpx;">暂无数据</text>
					</uni-forms-item>
				</uni-forms>
			</view>
			<view class="" style="display: flex;position: absolute;width: 100%;bottom: 20rpx;">
				<button type="primary" style="width: 200rpx;" @click="goImage">图片上传</button>
				<button type="primary" style="width: 200rpx;" @click="submit">提交</button>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		getPatrolPlan,
	} from "@/api/comp/patrolPlan";
	import {
		addPatrolEnter,
		queryPatrolRecord,
	} from "@/api/comp/patrolEnter";
	import {
		baseUrl
	} from "@/config.js";
	import {
		getToken
	} from '@/utils/auth'
	export default {
		data() {
			return {
				date: new Date(),
				// showCalendar: false,
				value: '',
				range: [{
					"value": 1,
					"text": "是"
				}, {
					"value": 0,
					"text": "否"
				}, ],
				formData: {
					xclr: '',
					xcjd: '',
					xcsm: ''
				},
				rules: {
					isAbnormal: {
						rules: [{
							required: true,
							errorMessage: '不能为空',
						}]
					},
					patrolTime: {
						rules: [{
							required: true,
							errorMessage: '不能为空',
						}]
					},
					// 对name字段进行必填验证
					patrolRecord: {
						rules: [{
								required: true,
								errorMessage: '不能为空',
							},
						]
					}
				},
				patrolPlanInfo: {},
				presentVerifyValid: true,
			}
		},
		onLoad(options) {
			this.getInfo(options)
		},
		methods: {
			getInfo(options) {
				getPatrolPlan(options.id).then(res => {
					if (res.data) {
						this.patrolPlanInfo = res.data
						console.log(this.patrolPlanInfo)
					}
				})
			},
			// 判断该计划日期是否已经录入
			async presentVerify() {
				let time = new Date(
					this.parseTime(this.formData.patrolTime, "{y}-{m}-{d}")
				).getTime();
				let res = await queryPatrolRecord();
				for (const item of res.rows) {
					let patrolTime = this.parseTime(item.patrolTime, "{y}-{m}-{d}");
					if (
						this.patrolPlanInfo.id === item.planId &&
						time === new Date(patrolTime).getTime()
					) {
						this.presentVerifyValid = false;
						break;
					} else {
						this.presentVerifyValid = true;
					}
				}
			},
			goImage() {
				uni.navigateTo({
					url: '/pages/gardens/xcjh/image'
				})
			},
			backClick() {
				uni.navigateBack()
			},
			uploadfn(tempFilePaths, id) {
				return new Promise((resolve, reject) => {
					uni.uploadFile({
						url: baseUrl + '/am/amAttach/uploadAndSaveTheFile', //仅为示例，非真实的接口地址
						filePath: tempFilePaths,
						name: 'file',
						header: {
							Authorization: 'Bearer ' + getToken()
						},
						formData: {
							mainId: id,
							bussType: 'td_comp_patrol_enter'
						},
						success: (uploadFileRes) => {
							console.log(uploadFileRes.data);
							resolve(uploadFileRes.data)
						},

						fail: function(err) {
							reject(err)
						}
					});
				})
			},
			generateRandomNumber() {
				const min = 10000; // 最小值（包含）
				const max = 99999; // 最大值（包含）
				return Math.floor(Math.random() * (max - min + 1)) + min + '';
			},
			submit() {
				this.$refs.form.validate().then(async res => {
					await this.presentVerify();
					if (!this.presentVerifyValid) {
						this.$u.toast("该日期已录入");
						return;
					}
					res.patrolTime = new Date(res.patrolTime).getTime()
					res.planId = this.patrolPlanInfo.id
					res.userName = this.$store.state.user.name
					let imgId = res.id = new Date().getTime() + this.generateRandomNumber()
					res.patrolPersonId = this.patrolPlanInfo.remark
					let imglist = this.$store.state.yhrw.xcImglist
					addPatrolEnter(res).then(res => {
						let arr = imglist.map(file => {
							return this.uploadfn(file, imgId)
						})
						this.$u.toast('提交成功')
						setTimeout(() => {
							uni.navigateBack()
						}, 2000)
					}).catch(_ => {
						this.$u.toast('提交失败')
					})
				}).catch(err => {
					// console.log('表单错误信息：', err);
					// this.$u.toast('查询条件为空，查询错误')
				})
			}

		}
	}
</script>

<style lang="scss">
	$nav-height: 30px;

	.box-bg {
		background-color: #F5F5F5;
		padding: 5px 0;
	}

	.city {
		/* #ifndef APP-PLUS-NVUE */
		display: flex;
		/* #endif */
		flex-direction: row;
		align-items: center;
		justify-content: flex-start;
		// width: 160rpx;
		margin-left: 4px;
	}

	.input-uni-icon {
		line-height: $nav-height;
	}

	.gradens-yhrw-view-card {
		box-sizing: border-box;
		padding: 10rpx 10px;
	}

	.gradens-xcjh-report {
		.yhrw-view-list-item {
			display: flex;
			padding-top: 20rpx;
			align-items: center;

			.yhrw-view-list-name {
				width: 200rpx;
			}

			.yhrw-view-list-text {
				flex: 1;
			}
		}
	}
</style>